1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#![allow(non_camel_case_types, non_snake_case)]

use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
use crate::prelude::*;

impl_handle! { HEVENT;
	/// Handle to a named or unnamed
	/// [event](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventw)
	/// object. Originally just a `HANDLE`.
}

impl kernel_Hevent for HEVENT {}

/// This trait is enabled with the `kernel` feature, and provides methods for
/// [`HEVENT`](crate::HEVENT).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait kernel_Hevent: Handle {
	/// [`CreateEvent`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventw)
	/// function.
	#[must_use]
	fn CreateEvent(
		security_attributes: Option<&mut SECURITY_ATTRIBUTES>,
		manual_reset: bool,
		initial_state: bool,
		name: Option<&str>,
	) -> SysResult<CloseHandleGuard<HEVENT>>
	{
		unsafe {
			ptr_to_sysresult_handle(
				ffi::CreateEventW(
					security_attributes.map_or(std::ptr::null_mut(), |sa| sa as *const _ as _),
					manual_reset as _,
					initial_state as _,
					WString::from_opt_str(name).as_ptr(),
				)
			).map(|h| CloseHandleGuard::new(h))
		}
	}

	/// [`CreateEventEx`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventexw)
	/// method.
	#[must_use]
	fn CreateEventEx(
		security_attributes: Option<&mut SECURITY_ATTRIBUTES>,
		name: Option<&str>,
		flags: co::CREATE_EVENT,
		desired_access: co::EVENT_RIGHTS,
	) -> SysResult<CloseHandleGuard<HEVENT>>
	{
		unsafe {
			ptr_to_sysresult_handle(
				ffi::CreateEventExW(
					security_attributes.map_or(std::ptr::null_mut(), |sa| sa as *const _ as _),
					WString::from_opt_str(name).as_ptr(),
					flags.raw(),
					desired_access.raw(),
				)
			).map(|h| CloseHandleGuard::new(h))
		}
	}

	/// [`OpenEvent`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-openeventw)
	/// function.
	#[must_use]
	fn OpenEvent(&self,
		desired_access: co::EVENT_RIGHTS,
		inherit_handle: bool,
		name: &str,
	) -> SysResult<CloseHandleGuard<HEVENT>>
	{
		unsafe {
			ptr_to_sysresult_handle(
				ffi::OpenEventW(
					desired_access.raw(),
					inherit_handle as _,
					WString::from_str(name).as_ptr(),
				)
			).map(|h| CloseHandleGuard::new(h))
		}
	}

	/// [`PulseEvent`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-pulseevent)
	/// function.
	fn PulseEvent(&self) -> SysResult<()> {
		bool_to_sysresult(unsafe { ffi::PulseEvent(self.ptr()) })
	}

	/// [`ResetEvent`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-resetevent)
	/// function.
	fn ResetEvent(&self) -> SysResult<()> {
		bool_to_sysresult(unsafe { ffi::ResetEvent(self.ptr()) })
	}

	/// [`SetEvent`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setevent)
	/// function.
	fn SetEvent(&self) -> SysResult<()> {
		bool_to_sysresult(unsafe { ffi::SetEvent(self.ptr()) })
	}

	/// [`WaitForSingleObject`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject)
	/// function.
	fn WaitForSingleObject(&self,
		milliseconds: Option<u32>,
	) -> SysResult<co::WAIT>
	{
		match unsafe {
			co::WAIT::from_raw(
				ffi::WaitForSingleObject(
					self.ptr(),
					milliseconds.unwrap_or(INFINITE),
				),
			)
		} {
			co::WAIT::FAILED => Err(GetLastError()),
			wait => Ok(wait),
		}
	}
}